package cn.zyq.ykb.service.config.impl;

import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.config.ConfigComplaintMapper;
import cn.zyq.ykb.beans.config.ConfigComplaint;
import cn.zyq.ykb.service.config.ConfigComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 配置-投诉建议Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-24
 */
@Service
public class ConfigComplaintServiceImpl  extends BaseService implements ConfigComplaintService {

    @Autowired
    private ConfigComplaintMapper configComplaintMapper;

    /**
     * 查询配置-投诉建议
     *
     * @param configComplaintID 配置-投诉建议主键
     * @return 配置-投诉建议
     */
    @Override
    public ConfigComplaint selectConfigComplaintByConfigComplaintID(Integer configComplaintID) {
        ConfigComplaint configComplaint = configComplaintMapper.selectByPrimaryKey(configComplaintID);
        if (configComplaint.getDeleted() == 1) {
            return null;
        }
        return configComplaint;
    }

    /**
     * 查询配置-投诉建议
     *
     * @param serviceID 业务ID
     * @return 配置-投诉建议
     */
    @Override
    public ConfigComplaint selectConfigComplaintByServiceID(String serviceID) {
        ConfigComplaint param = new ConfigComplaint();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.configComplaintMapper.selectOne(param);
    }

    /**
     * 查询配置-投诉建议列表
     *
     * @param pageParam
     * @return 配置-投诉建议
     */
    @Override
    public PageInfo<ConfigComplaint> selectConfigComplaintList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<ConfigComplaint> list = this.configComplaintMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询配置-投诉建议列表
     *
     * @param param
     * @return 配置-投诉建议
     */
    @Override
    public List<ConfigComplaint> selectConfigComplaintList(ConfigComplaint param) {
        param.setDeleted(0);
        return this.configComplaintMapper.select(param);
    }
    /**
     * 新增配置-投诉建议
     *
     * @param param 配置-投诉建议
     * @return 结果
     */
    @Override
    public int insertConfigComplaint(ConfigComplaint param, User loginUser) {
        this.setCreateBaseData(param,loginUser);
        return this.configComplaintMapper.insertSelective(param);
    }

    /**
     * 修改配置-投诉建议
     *
     * @param param 配置-投诉建议
     * @return 结果
     */
    @Override
    public int updateConfigComplaint(ConfigComplaint param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.configComplaintMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除配置-投诉建议
     *
     * @param configComplaintIDs 需要删除的配置-投诉建议主键
     * @return 结果
     */
    @Override
    public int deleteConfigComplaintByConfigComplaintIDs(Integer[] configComplaintIDs) {
        int i = 0;
        for (Integer configComplaintID : configComplaintIDs) {
            i += this.deleteConfigComplaintByConfigComplaintID(configComplaintID);
        }
        return i;
    }

    /**
     * 删除配置-投诉建议信息
     *
     * @param configComplaintID 配置-投诉建议主键
     * @return 结果
     */
    @Override
    public int deleteConfigComplaintByConfigComplaintID(Integer configComplaintID) {
        ConfigComplaint configComplaint = this.selectConfigComplaintByConfigComplaintID(configComplaintID);
        if (configComplaint == null) {
            return 0;
        } else {
            ConfigComplaint storeInfo = new ConfigComplaint();
            storeInfo.setConfigComplaintID(configComplaintID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.configComplaintMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
